﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Rect = System.Drawing.Rectangle;
using LuaInterface;
using System.IO;

namespace LeagueOverlay
{
    public class LeagueUI
    {
        public static Rect
            mapR,

            cLevel,
            cHealth,
            cMana,

            cAbility1R,
            cAbility2R,
            cAbility3R,
            cAbility4R,

            sAbility1R,
            sAbility2R,

            ally1R,
            ally2R,
            ally3R,
            ally4R,

            ab1Plus,
            ab2Plus,
            ab3Plus,
            ab4Plus,

            playerAvatar;
        public static Rect[] spellRect = new Rect[6];
        public static MainWindow parent;
        public static int xResolution, yResolution, levelBitSize = 0;
        public static byte[][] levelBmBytes = new byte[18][];
        public static Dictionary<int, Rect> levelRects = new Dictionary<int, Rect>();

        public static void setMainWindow(MainWindow mw)
        {
            parent = mw;
        }

        public static void loadLevelBitmaps()
        {

            try
            {
                Bitmap[] levelBitmaps = new Bitmap[18];
                System.Drawing.Imaging.BitmapData bd;
                IntPtr ip;
                int index;
                foreach (FileInfo f in (new DirectoryInfo("levelImages")).GetFiles())
                {
                    string[] split = f.Name.Split("_.".ToCharArray());
                    index = Convert.ToInt32(split[1]) - 1;
                    levelBitmaps[index] = new Bitmap(new Bitmap(f.FullName).Clone(new Rect(3, 3, (12), (8)), System.Drawing.Imaging.PixelFormat.Undefined));
                    bd = levelBitmaps[index].LockBits(new System.Drawing.Rectangle(0, 0, 12, 8), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    ip = bd.Scan0;
                    levelBitSize = bd.Stride * levelBitmaps[index].Height;
                    levelBmBytes[index] = new byte[levelBitSize];
                    System.Runtime.InteropServices.Marshal.Copy(ip, levelBmBytes[index], 0, levelBitSize);
                    levelBitmaps[index].UnlockBits(bd);
                }
            }
            catch (Exception e)
            {
                new ScriptControl(null).log("[ERROR] (" + e.Source + "): " + e.Message + "\n" + e.StackTrace);
                throw;
            }

            /*for (int i = 0; i < levelBitmaps.Length; i++)
                levelBitmaps[i].Save("championInfo\\level" + i + ".bmp");*/
        }

        //calculate all of the locations for screen elements
        public static void init(int xRes, int yRes)
        {
            try
            {
                //scalings didn't work well enough, so hardcoded it is...
                levelRects[1024] = new Rect((int)Math.Round(60 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((768 - 742) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(10 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(8 * LeagueUtils.Preferences.GameGlobalScale));// 1024x768
                levelRects[1152] = new Rect((int)Math.Round(69 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((864 - 836) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(11 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(8 * LeagueUtils.Preferences.GameGlobalScale));// 1152 864
                levelRects[1280] = new Rect((int)Math.Round(76 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((768 - 737) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(13 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(10 * LeagueUtils.Preferences.GameGlobalScale));//1280x768
                levelRects[1360] = new Rect((int)Math.Round(76 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((1024 - 992) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(12 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Ceiling(9 * LeagueUtils.Preferences.GameGlobalScale)); // 1280x1024
                levelRects[1400] = new Rect((int)Math.Round(83 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((1050 - 1016) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(14 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(10 * LeagueUtils.Preferences.GameGlobalScale));
                levelRects[1440] = new Rect((int)Math.Round(97 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((900 - 865) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(13 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(10 * LeagueUtils.Preferences.GameGlobalScale));
                levelRects[1600] = new Rect((int)Math.Round(95 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((1200 - 1161) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((16) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((12) * LeagueUtils.Preferences.GameGlobalScale));// 1600x1200
                levelRects[1680] = new Rect((int)Math.Round(62 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((1050 - 1024) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(11 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(7 * LeagueUtils.Preferences.GameGlobalScale));
                levelRects[1776] = new Rect((int)Math.Round(101 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((1000 - 958) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(16 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(11 * LeagueUtils.Preferences.GameGlobalScale));
                levelRects[1920] = new Rect((int)Math.Round(101 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round((1080 - 1038) * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(16 * LeagueUtils.Preferences.GameGlobalScale), (int)Math.Round(11 * LeagueUtils.Preferences.GameGlobalScale));

                xResolution = xRes;
                yResolution = yRes;

                double widthScale = xRes / 1280.0;
                double heightScale = yRes / 758.0;

                int plusSize = (int)Math.Round(16 * LeagueUtils.Preferences.GameGlobalScale * widthScale);
                int abilitySize = (int)Math.Round(37 * LeagueUtils.Preferences.GameGlobalScale * widthScale);
                //int firstXPos = (int)Math.Round(507 * LeagueUtils.Preferences.GameGlobalScale * widthScale);
                int abilityHeight = (int)Math.Round(yRes - (102 * LeagueUtils.Preferences.GameGlobalScale) * widthScale);

                int avatarSize = (int)Math.Round(102 * LeagueUtils.Preferences.GameGlobalScale * widthScale);
                int abPlusHeight = (int)Math.Round(yRes - (142 * LeagueUtils.Preferences.GameGlobalScale) * widthScale) - (int)(Math.Ceiling(LeagueUtils.Preferences.GameGlobalScale / widthScale));
                int clevelHeight = (int)Math.Floor(yRes - (31 * LeagueUtils.Preferences.GameGlobalScale) * widthScale) - 1;
                int playerAvatarHeight = (int)Math.Round(yRes - (116 * LeagueUtils.Preferences.GameGlobalScale) * widthScale);
                //3 3 - 14 10




                //Since not all resolutions are supported, just use 1280 for default so nothing crashes...
                if (levelRects.Keys.Contains(xRes))
                    cLevel = new Rect(levelRects[xRes].X, yRes - levelRects[xRes].Y, levelRects[xRes].Width, levelRects[xRes].Height);
                else
                {
                    cLevel = new Rect(levelRects[1280].X, yRes - levelRects[1280].Y, levelRects[1280].Width, levelRects[1280].Height);
                }

                /*
                 * Locations for each spell rectangle, modified by the UI scale and resolution
                 */
                spellRect[0] = new Rect((int)Math.Round((xRes / 2) - (130 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                spellRect[1] = new Rect((int)Math.Round((xRes / 2) - (87 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                spellRect[2] = new Rect((int)Math.Round((xRes / 2) - (45 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                spellRect[3] = new Rect((int)Math.Round((xRes / 2) - (2 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                cAbility1R = new Rect((int)Math.Round((xRes / 2) - (130 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                cAbility2R = new Rect((int)Math.Round((xRes / 2) - (87 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                cAbility3R = new Rect((int)Math.Round((xRes / 2) - (45 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                cAbility4R = new Rect((int)Math.Round((xRes / 2) - (2 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);


                spellRect[4] = new Rect((int)Math.Round((xRes / 2) + (52 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                spellRect[5] = new Rect((int)Math.Round((xRes / 2) + (95 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                sAbility1R = new Rect((int)Math.Round((xRes / 2) + (52 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);
                sAbility2R = new Rect((int)Math.Round((xRes / 2) + (95 * LeagueUtils.Preferences.GameGlobalScale * widthScale)), abilityHeight, abilitySize, abilitySize);

                ab1Plus = new Rect((int)Math.Round(522 * widthScale * LeagueUtils.Preferences.GameGlobalScale), abPlusHeight, plusSize, plusSize);
                ab2Plus = new Rect((int)Math.Round(562 * widthScale * LeagueUtils.Preferences.GameGlobalScale), abPlusHeight, plusSize, plusSize);
                ab3Plus = new Rect((int)Math.Round(602 * widthScale * LeagueUtils.Preferences.GameGlobalScale), abPlusHeight, plusSize, plusSize);
                ab4Plus = new Rect((int)Math.Round(642 * widthScale * LeagueUtils.Preferences.GameGlobalScale), abPlusHeight, plusSize, plusSize);

                cHealth = new Rect(
                    (int)Math.Round((xRes / 2) - (190 * LeagueUtils.Preferences.GameGlobalScale * widthScale)),
                    (int)Math.Round(yRes - (50 * LeagueUtils.Preferences.GameGlobalScale) * widthScale),
                    (int)Math.Round(381 * LeagueUtils.Preferences.GameGlobalScale * widthScale),
                    (int)Math.Round(14 * LeagueUtils.Preferences.GameGlobalScale * widthScale));
                cMana = new Rect(
                    (int)Math.Round((xRes / 2) - (190 * LeagueUtils.Preferences.GameGlobalScale * widthScale)),
                    (int)Math.Round(yRes - (30 * LeagueUtils.Preferences.GameGlobalScale) * widthScale),
                    (int)Math.Round(381 * LeagueUtils.Preferences.GameGlobalScale * widthScale),
                    (int)Math.Round(14 * LeagueUtils.Preferences.GameGlobalScale * widthScale));

                playerAvatar = new Rect((int)Math.Round(9 * widthScale * LeagueUtils.Preferences.GameGlobalScale), playerAvatarHeight, avatarSize, avatarSize);
                // cLevel = new Rect((int)Math.Floor(76 * widthScale), clevelHeight, cLevelWidth, cLevelHeight0);

                if (parent.leagueInfo.outOfLoadScreen)
                    parent.scriptControl.raiseEvent("interfaceInit", "");
            }
            catch (Exception e)
            {
                new ScriptControl(null).log("[ERROR] (" + e.Source + "): " + e.Message + "\n" + e.StackTrace);
                throw;
            }
        }


        //functions for getting UI element positions in LUA
        [AttrLuaFunc("GetResolutionX")]
        public int getResolutionX() { return xResolution; }
        [AttrLuaFunc("GetResolutionY")]
        public int getResolutionY() { return yResolution; }
        [AttrLuaFunc("GetGlobalScale")]
        public double getGlobalScale() { return LeagueUtils.Preferences.GameGlobalScale; }

        [AttrLuaFunc("GetUIRect_Map")]
        public void LUA_getmapR(LuaTable table) { parent.scriptControl.storeRectInTable(mapR, table); }
        [AttrLuaFunc("GetUIRect_ChampionLevel")]
        public void LUA_getcLevel(LuaTable table) { parent.scriptControl.storeRectInTable(cLevel, table); }
        [AttrLuaFunc("GetUIRect_ChampionHealth")]
        public void LUA_getcHealth(LuaTable table) { parent.scriptControl.storeRectInTable(cHealth, table); }
        [AttrLuaFunc("GetUIRect_ChampionMana")]
        public void LUA_getcMana(LuaTable table) { parent.scriptControl.storeRectInTable(cMana, table); }
        [AttrLuaFunc("GetUIRect_ChampionAbility1")]
        public void LUA_getcAbility1R(LuaTable table) { parent.scriptControl.storeRectInTable(cAbility1R, table); }
        [AttrLuaFunc("GetUIRect_ChampionAbility2")]
        public void LUA_getcAbility2R(LuaTable table) { parent.scriptControl.storeRectInTable(cAbility2R, table); }
        [AttrLuaFunc("GetUIRect_ChampionAbility3")]
        public void LUA_getcAbility3R(LuaTable table) { parent.scriptControl.storeRectInTable(cAbility3R, table); }
        [AttrLuaFunc("GetUIRect_ChampionAbility4")]
        public void LUA_getcAbility4R(LuaTable table) { parent.scriptControl.storeRectInTable(cAbility4R, table); }
        [AttrLuaFunc("GetUIRect_SummonerAbility1")]
        public void LUA_getsAbility1R(LuaTable table) { parent.scriptControl.storeRectInTable(sAbility1R, table); }
        [AttrLuaFunc("GetUIRect_SummonerAbility2")]
        public void LUA_getsAbility2R(LuaTable table) { parent.scriptControl.storeRectInTable(sAbility2R, table); }
        [AttrLuaFunc("GetUIRect_PlayerAvatar")]
        public void LUA_getplayerAvatar(LuaTable table) { parent.scriptControl.storeRectInTable(playerAvatar, table); }

        [AttrLuaFunc("GetUIRect_AbilityByNum")]
        public LuaTable LUA_getAbilityByNum(int num, LuaTable table)
        {
            switch (num)
            {
                case 0: //ChampionAbility1
                    return parent.scriptControl.storeRectInTable(cAbility1R, table);
                case 1: //ChampionAbility2
                    return parent.scriptControl.storeRectInTable(cAbility2R, table);
                case 2: //ChampionAbility3
                    return parent.scriptControl.storeRectInTable(cAbility3R, table);
                case 3: //ChampionAbility4
                    return parent.scriptControl.storeRectInTable(cAbility4R, table);
                case 4: //SummonerSpell1
                    return parent.scriptControl.storeRectInTable(sAbility1R, table);
                case 5: //SummonerSpell2
                    return parent.scriptControl.storeRectInTable(sAbility2R, table);
            }
            return parent.scriptControl.storeRectInTable(new Rect(), table);
        }

    }
}
